package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishflavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishflavorMapper dishflavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * /**
     * 新增菜品
     * @param dto
     */
    @Transactional //0.涉及到多张表的增删改，需开启事务
    public void addDish(DishDTO dto) {
        //1.构造菜品基本数据，存入dish表中
        Dish dish = new Dish();
        //拷贝属性值
        BeanUtils.copyProperties(dto,dish);
        //调用mapper，保存方法
        dishMapper.insert(dish);


        log.info("dishID={}",dish.getId());


        //2.构造菜品口味数据，存入dish_flavor表中
        List<DishFlavor> dishFlavorList = dto.getFlavors();
        //2.1关联菜品id
        dishFlavorList.forEach(flavor -> flavor.setDishId(dish.getId()));
        //2.2调用mapper保存方法
        dishflavorMapper.insertBatch(dishFlavorList);
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dto) {

        //1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //2.调用mapper的列表查询方法，强转成page
        Page<DishVO> page = dishMapper.list(dto);
        //3。封装PageResult对象，返回
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     *  根据id删除菜品
     * @param ids
     */
    @Transactional
    public void delete(List<Long> ids) {
        //1.删除菜品之前，需要判断菜品是否起售，起售中的不允许删除
        ids.forEach(id -> {
         Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //当前菜品处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //2.需要判断菜品是否被套餐关联，关联了不允许删除
         Integer count   = setmealDishMapper.countByDishIds(ids);
         if (count > 0) {
             throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
         }

        //3.删除菜品基本信息dish表
        dishMapper.deleteBatch(ids);

        //4.删除菜品口味列表信息 dish_flavor表
        dishflavorMapper.deleteBatch(ids);
    }

    /**
     * 根据id回显菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();
        //1.根据菜品id查询菜品基本信息
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);

        //2.根据菜品id查询菜品口味列表信息
        List<DishFlavor> flavors = dishflavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);

        //3.构造DishVo对象并返回
        return dishVO;
    }

    @Transactional
    public void update(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        //1.修改菜品的基本信息，dish表
        dishMapper.update(dish);
        //2.修改菜品口味列表信息，dish_flavor表
        //由于口味数据可能增加、删除、修改，所以先全部删除旧数据，再添加新数据
        dishflavorMapper.deleteByDishId(dto.getId());
        //重新插入口味数据
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dto.getId());
            });
            //向口味表插入n条数据
            dishflavorMapper.insertBatch(flavors);
        }

    }
}
